var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cache = require ( 'memory-cache' ) ;
var session = require("express-session");


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var loginRouter = require('./routes/login');
//var sendEmailRouter = require('./routes/sendEmail');
var smRouter = require('./routes/sm');
var smDict = require('./config/smDict.js');//解析参数
var getuserRouter = require('./routes/getuser');
var logoutRouter = require('./routes/logout');
var sendEmailRouter = require('./routes/sendEmail');

var session = require("express-session");


cache.put('smDict', smDict) ;
var app = express();
//配置中间件
app.use(session({
  secret: 'oceansky',   // 可以随便写。 一个 String 类型的字符串，作为服务器端生成 session 的签名
  name:'session_id',/*保存在本地cookie的一个名字 默认connect.sid  可以不设置*/
  resave: false,   /*强制保存 session 即使它并没有变化,。默认为 true。建议设置成 false。*/
  saveUninitialized: true,   //强制将未初始化的 session 存储。  默认值是true  建议设置成true
  cookie: {
      maxAge:1000*30*60    /*过期时间*/
  },   /*secure https这样的情况才可以访问cookie*/
  //设置过期时间比如是30分钟，只要游览页面，30分钟没有操作的话在过期
  rolling:true //在每次请求时强行设置 cookie，这将重置 cookie 过期时间（默认：false）
}))

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/login.ocean', loginRouter);
//app.use('/sendEmail', sendEmailRouter);
app.use('/sm', smRouter);
app.use('/getuser', getuserRouter);
app.use('/logout', logoutRouter);
app.use('/sendEmail', sendEmailRouter);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
